package Thread;

import java.util.concurrent.CyclicBarrier;

/***
 * 相当与计数器，与CountDownLatch相似
 * By LIBOFEI
 * */
class write extends Thread{

    private CyclicBarrier cyclicBarrier;

    public write(CyclicBarrier cyclicBarrier){
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        System.out.println(currentThread().getName() + "开始写入数据....");
        try{
            //模拟损耗时间
            Thread.sleep(30);
            System.out.println(currentThread().getName() + "写入数据成功....");

            //让五个线程全部执行完以上代码之后再执行以下代码
            cyclicBarrier.await();
            System.out.println(currentThread().getName() + "全部动作执行完毕....");
        }catch (Exception e){

        }

    }
}

public class ThreadCurrentCyclicBarrier {
    public static void main(String[] args) {
        //相当于一个计数功能
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        for(int i = 0 ; i < 5 ; i++){
            new write(cyclicBarrier).start();
        }
    }
}
